package com.example.code;

import com.example.exception.GeneratorException;
import com.example.model.Model;
import com.example.model.PathConfig;
import com.example.model.ProjectConfig;
import com.example.util.CodeUtil;
import com.example.util.ConfigUtil;

import java.util.List;
import java.util.Map;

public class CoreCode {

    /**
     * Description: 生成Service
     * @author: 王培喜(58528792@163.com)
     * @date:  2021/8/25 16:18
     */
    public static void getService(Model model, String author, ProjectConfig projectConfig, String fileName, String type, String filterName) {

        Map<String, Object> data = CodeUtil.setParam(model, author, projectConfig);
        String domainNameLowerCamel = data.get("domainNameLowerCamel").toString();
        String domainNameUpperCamel = data.get("domainNameUpperCamel").toString();
        //过滤表前缀
        filterName = ConfigUtil.filterTableName(model.getTableName(), filterName);
        if(null != filterName){
            domainNameLowerCamel = ConfigUtil.camelCaseName(model.getTableName().replace(filterName,""));
            data.put("domainNameLowerCamel",domainNameLowerCamel);
            domainNameUpperCamel = domainNameLowerCamel.substring(0,1).toUpperCase()+ domainNameLowerCamel.substring(1);
            data.put("domainNameUpperCamel",domainNameUpperCamel);
        }
        //持久类型
        if(!type.isEmpty() && type.equalsIgnoreCase("dao")){
            data.put("interfaceType",type);
            data.put("interface",type.toLowerCase());
        }else{
            data.put("interfaceType","Mapper");
            data.put("interface","mapper");
        }
        data.put("entity","Entity");
        String savePath, savePath1, tempName, tempName1;
            savePath = fileName + PathConfig.service + data.get("domainNameUpperCamel") + "Service.java";
            tempName = "code/service.ftl";

            savePath1 = fileName + PathConfig.serviceImpl + data.get("domainNameUpperCamel") + "ServiceImpl.java";
            tempName1 = "code/service-impl.ftl";

        try {
            CodeUtil.tempFactory(savePath, tempName, data);
            System.out.println(data.get("domainNameUpperCamel") + "Service.java 生成成功");

            CodeUtil.tempFactory(savePath1, tempName1, data);
            System.out.println(data.get("domainNameUpperCamel") + "ServiceImpl.java 生成成功");
        } catch (Exception e) {
            throw new GeneratorException("生成Service失败");
        }

    }
    /**
     * Description: 生成Facade
     * @author: 王培喜(58528792@163.com)
     * @date:  2021/8/25 16:18
     */
    public static void getFacade(Model model, String author,ProjectConfig projectConfig, String fileName, String type, String filterName) {
        Map<String, Object> data = CodeUtil.setParam(model, author, projectConfig);
        String domainNameLowerCamel = data.get("domainNameLowerCamel").toString();
        String domainNameUpperCamel = data.get("domainNameUpperCamel").toString();
        filterName = ConfigUtil.filterTableName(model.getTableName(), filterName);
        if(null != filterName){
            domainNameLowerCamel = ConfigUtil.camelCaseName(model.getTableName().replace(filterName,""));
            data.put("domainNameLowerCamel",domainNameLowerCamel);
            domainNameUpperCamel = domainNameLowerCamel.substring(0,1).toUpperCase()+ domainNameLowerCamel.substring(1);
            data.put("domainNameUpperCamel",domainNameUpperCamel);
        }
        String savePath, savePath1, tempName, tempName1;
            savePath = fileName + PathConfig.facade + data.get("domainNameUpperCamel") + "Facade.java";
            tempName = "code/facade.ftl";

            savePath1 = fileName + PathConfig.facadeImpl + data.get("domainNameUpperCamel") + "FacadeImpl.java";
            tempName1 = "code/facade-impl.ftl";

        try {
            CodeUtil.tempFactory(savePath, tempName, data);
            System.out.println(data.get("domainNameUpperCamel") + "Facade.java 生成成功");

            CodeUtil.tempFactory(savePath1, tempName1, data);
            System.out.println(data.get("domainNameUpperCamel") + "FacadeImpl.java 生成成功");
        } catch (Exception e) {
            throw new GeneratorException("生成Facade失败");
        }

    }


}
